package com.wowudi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wowudi.mapper.PopedomMapper;
import com.wowudi.mapper.UserMapper;
import com.wowudi.model.power.Popedom;
import com.wowudi.model.power.User;
import com.wowudi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PopedomMapper popedomMapper;

    @Override
    public HashMap<String,Object> login(String account, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("u_account",account).eq("u_password",password);
        User user = userMapper.selectOne(wrapper);

        HashMap<String, Object> hashMap = new HashMap<>();
        if (user != null){
            List<Popedom> popedoms = popedomMapper.selectAllByUId(user.getUId());
            hashMap.put("popedoms",popedoms);
        }
        hashMap.put("user",user);

        return hashMap;
    }

    @Override
    public Page<User> findList(Integer page, Integer limit) {
        Page<User> userPage = new Page<>(page,limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("u_state",1);
        userMapper.selectPage(userPage,wrapper);
        return userPage;
    }

    @Override
    public int add(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int update(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteById(int id) {
        return userMapper.deleteById(id);
    }
}
